﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text;

public partial class DataServiceForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userID"] == null)
        {
            //Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script language=javascript>window.setTimeout(window.location.href = 'Default.aspx' ,5000);</script>");
            // Response.Redirect("Default.aspx");
        }
        Session["where"] = this.ToString();
        string app = Request.Form["app"];
        if (app == "contact")
        {
            Contact();
        }
        else if (app == "calendar")
        {
            Calendar();
        }
    }
    #region appContact
    protected void Contact()
    {
        string func = Request.Form["func"];
        if (func == "query")
        {
            contactQuery();
        }
        else if (func == "insert")
        {
            contactInsert();
        }
        else if (func == "delete")
        {
            contactDelete();
        }
    }
    protected void contactQuery()
    {
        DbConnect conn = new DbConnect();
        conn.query = "SELECT a.ContactName,b.User_ID FROM Contacts	a, Users b " +
        "WHERE a.Username='" + Session["userName"] + "' and a.ContactName = b.Username";
        string result = conn.GetResult();
        Response.Write(result);
    }
    protected string contactPreQuery(string user)
    {
        //PreCheck for Insert Contact
        string res = string.Empty;
        DbConnect preConn = new DbConnect();
        preConn.query = "SELECT count(Username) FROM Users WHERE Username = '"+ user +"'";
        string result = preConn.GetResult();
        if (result != null)
            res = result;
        return res;
    }
    protected void contactInsert()
    {
        string test = string.Empty;
        string user = Request.Form["user"];
        test = contactPreQuery(user);
        DbConnect conn = new DbConnect();
        //conn.query = "INSERT INTO Contacts values('"+ Session["userName"] +"','"+ user +"')";
        conn.query = "IF EXISTS ( select Username from Users Where Username = '" + user + "')" +
            "BEGIN INSERT INTO Contacts values('" + Session["userName"] + "','" + user + "') END";
        conn.Update();
        /*if(test.Equals("ok"))
            Response.Write("<conn name='msg'>Add Success</conn>");
        else*/
            Response.Write("<conn name='msg'>"+test+"</conn>");
    }
    protected void contactDelete()
    {
        string user = Request.Form["user"];
        DbConnect conn = new DbConnect();
        conn.query = "DELETE FROM Contacts WHERE Username = '" + Session["userName"] + "' and ContactName = '" + user + "'";
        conn.Update();
        Response.Write("<conn name='msg'>Delete Success</conn>");
    }
    #endregion

    #region appCalendar
    protected void Calendar()
    {
        string func = Request.Form["func"];
        if (func == "query")
        {
            calendarQuery();
        }
        else if (func == "insert")
        {
            calendarInsert();
        }
        else if (func == "delete")
        {
            calendarDelete();
        }
    }
    protected void calendarQuery()
    {
        string date = Request.Form["date"];
        int month = System.Convert.ToInt32(Request.Form["month"], 10) + 1;
        string year = Request.Form["year"];
        DbConnect conn = new DbConnect();
        conn.query = "SELECT datepart(dd,Date) as date,datepart(mm,Date) as month,"+
	    "datepart(yyyy,Date) as year,Time_ID,[Subject],[Desc]"+ 
        "FROM Calendars WHERE User_ID ="+Session["userID"]+
        " and datepart(yyyy,Date) = "+year+" and datepart(mm,Date) = "+month+
        " and datepart(dd,Date) = "+date+"";
        string result = conn.GetResult();
        Response.Write(result);
    }
    protected void calendarInsert()
    {
        string date = Request.Form["date"];
        int month = System.Convert.ToInt32(Request.Form["month"], 10)+1;
        string year = Request.Form["year"];
        string time = Request.Form["time"];
        string subject = Request.Form["subject"];
        string desc = Request.Form["desc"];
        DbConnect conn = new DbConnect();
        conn.query = "INSERT INTO Calendars values ('"+Session["userID"]+"','"+time+"','"+month+"/"+date+"/"+year+"','"+subject+"','"+desc+"')";
        conn.Update();
    }
    protected void calendarDelete()
    {
        string user = Request.Form["user"];
        DbConnect conn = new DbConnect();
        conn.query = "";
        conn.Update();
    }
    #endregion
}
